package com.liu.mybatisPlus.wm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.mybatisPlus.wm.entity.base.WmItem;
import com.liu.mybatisPlus.wm.entity.base.WmItemKeyWord;
import com.liu.mybatisPlus.wm.mapper.WmItemKeyWordMapper;
import com.liu.mybatisPlus.wm.service.WmItemKeyWordService;
import com.liu.mybatisPlus.wm.service.WmItemService;
import com.liu.qqRobot.wm.entity.qo.AddWmItemKeyWordQO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuChengLong
 * @since 2025-04-01
 */
@Service
public class WmItemKeyWordServiceImpl extends ServiceImpl<WmItemKeyWordMapper, WmItemKeyWord> implements WmItemKeyWordService {

    @Resource
    private WmItemService wmItemService;

    @Override
    public List<WmItem> getKeyWordMatch(String keyWord) {
        return baseMapper.getKeyWordMatch(keyWord);
    }

    @Override
    public void add(List<AddWmItemKeyWordQO> qoList) {
        // 校验关键词是否存在
        List<WmItemKeyWord> insertList = new ArrayList<>();
        for (AddWmItemKeyWordQO qo : qoList) {
            WmItem wmItem = wmItemService.getById(qo.getItemId());
            if (wmItem == null) {
                continue;
            }
            QueryWrapper<WmItemKeyWord> wmItemKeyWordQueryWrapper = new QueryWrapper<>();
            wmItemKeyWordQueryWrapper.eq("key_word", qo.getKeyWord());
            WmItemKeyWord wmItemKeyWord = super.getOne(wmItemKeyWordQueryWrapper);
            if (wmItemKeyWord != null) {
                continue;
            }
            wmItemKeyWord = new WmItemKeyWord();
            wmItemKeyWord.setKeyWord(qo.getKeyWord());
            wmItemKeyWord.setId(qo.getItemId());
            insertList.add(wmItemKeyWord);
        }
        super.saveBatch(insertList);
    }

}
